package com.wonders.frame.aspect;

import com.wonders.frame.common.Enum.ResponseCodeEnum;
import com.wonders.frame.common.util.ServerUtil;
import com.wonders.frame.controller.core.AbstractController;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * @author qiu
 */
@Aspect
@Component
public class LoginAspect extends AbstractController {

    public static final Map<String, String> ACCOUNT_SESSION = new HashMap<>();

    @Pointcut(
            "within(com.wonders.frame.controller..*) " +
                    "&& !within(com.wonders.frame.controller.LoginController)" +
                    "&& !within(com.wonders.frame.controller.VerifyController)" +
                  //  "&& !within(com.wonders.frame.controller.AuthAccountController)"+
                    "&& !within(com.wonders.frame.controller.CommonSourceController)"
    )
    public void webRequest() {
    }

    @Around("webRequest()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();

        HttpServletRequest request = attributes.getRequest();
        if (ACCOUNT_SESSION.values().contains(request.getSession().getId())) {
            return pjp.proceed();
        }
        return getResponseWithCode(ResponseCodeEnum.NEED_LOGIN.getCode());
    }

}